<?php
            
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 抖音商城项目
 * Time: 2021/11/14 10:07 上午
 * Email: 17854288794@163.com
 */
class Index_Page extends PluginWebPage {

    function main()
    {
        global $_G,$_P,$_S,$_C;
        $action = 5;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }

        $name = isset($_P['name'])?trim($_P['name']):'';
        $page_index = max(1,intval($_P['page']));
        $sort = isset($_P['sort'])?$_P['sort']:'';
        $key = isset($_P['key'])?$_P['key']:1;
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $shangjiaid = isset($_P['shangjiaid'])?$_P['shangjiaid']:0;
        $shangjia = BeilaiModel::getShangjiaList();
        array_unshift($shangjia,['id'=>0,'name'=>'全部商家']);
        $page_total = BeilaiModel::getCourseList($themeid,$shangjiaid,$name,0,0,0,$sort,$key,true);
        $list = BeilaiModel::getCourseList($themeid,$shangjiaid,$name,$page_start,$page_size,0,$sort,$key);
        $total = $page_total;
        $pager = pagination($page_total,$page_index,$page_size);
        if ($key==1){
            $key=2;
        }else{
            $key=1;
        }
        $return_clicent = get_defined_vars();
return $this->view('/beilai/course/main', $return_clicent);
    }

    function add()
    {
        return $this->post();
    }

    function edit()
    {
        return $this->post();
    }

    protected function post()
    {
        global $_G, $_P;
        $action = 5;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);
        $shangjiaid = intval($_P['shangjiaid']);
        $shangjia = BeilaiModel::getShangjiaList();
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }

        if ($_G['ispost'] && $themeid ) {
            if (!$shangjiaid){
                show_json(0,'课程商家不正确');
            }
            $name = trim($_P['name']);
            $top_img = tomedia($_P['top_img']);
            $price = trim($_P['price']);
            $qty = isset($_P['qty'])?$_P['qty']:1000;
            $status = isset($_P['status'])?$_P['status']:1;
            $show_sort = isset($_P['show_sort'])?$_P['show_sort']:10;
            if (!$name){
                show_json(0,'课程名称不能为空');
            }
            if (!$top_img){
                show_json(0,'课程头图未设置');
            }
            if (!$qty){
                show_json(0,'课程当前库存未设置');
            }

            $data = [
                'shangjiaid'=>$shangjiaid,
                'name'=>$name,
                'top_img'=>$top_img,
                'qty'=>$qty,
                'show_sort'=>$show_sort,
                'price'=>$price,
                'status'=>$status,
                'themeid'=>$themeid,
            ];
            if (!$id){

                $res = BeilaiModel::insertCourse($data);
            }else{
                $where = ['id'=>$id];
                $res = BeilaiModel::editCourseItem($where,$data);
            }
            if (!$res){
                show_json(0,['msg'=>'操作失败']);
            }else{
                show_json(1,['msg'=>'操作成功']);
            }
            die();
        }
        if ($id && $themeid ){
            $item = BeilaiModel::getCourseItemById($themeid,$id);
        }
        $return_clicent = get_defined_vars();
return $this->view('/beilai/course/edit', $return_clicent);
    }

    function editItemQty()
    {
        global $_G, $_P;
        $action = 5;
        $id = intval($_P['id']);
        $themeid = intval($_P['themeid']);
        $op_num = intval($_P['op_num']);
        $action = trim($_P['action']);
        $new_qty = 0;

        $item = BeilaiModel::getCourseItemById($themeid,$id);
        if ($action == 'add'){
            $new_qty = $item['qty']+$op_num;
        }else{
            $new_qty = max(0,($item['qty']-$op_num));
        }
        $where = ['id'=>$id];
        $data = ['qty'=>$new_qty];
        $res = BeilaiModel::editCourseItem($where,$data);
        if (!$res){
            show_json(0,['msg'=>'操作失败']);
        }else{
            show_json(1,['msg'=>'操作成功']);
        }
    }

    function editSortItem()
    {
        global $_G,$_S,$_C,$_P;
        $system = $_S;
        $config = $_C;
        $p_id = intval($_P['id']);
        $themeid = $_P['themeid'];
        $action = $_P['action'];
        $sort = intval($_P['sort']);
        $temp_sort = 0;

        if ($action=='down'){
            $where = ['id'=>$p_id,'themeid'=>$themeid];
            $data = ['show_sort'=>$temp_sort];

            $where1 = ['show_sort'=>$sort+1,'themeid'=>$themeid];
            $data1 = ['show_sort'=>$sort];

            $where2 = ['id'=>$p_id,'themeid'=>$themeid];
            $data2 = ['show_sort'=>$sort+1];
        }
        if ($action=='up'){
            $where = ['id'=>$p_id,'themeid'=>$themeid];
            $where1 = ['show_sort'=>$sort-1,'themeid'=>$themeid];
            $data = ['show_sort'=>$temp_sort];
            $data1 = ['show_sort'=>$sort];
            $where2 = ['id'=>$p_id,'themeid'=>$themeid];
            $data2 = ['show_sort'=>$sort-1];
        }

        $res = BeilaiModel::editCourseItem($where,$data);
        $res1 = BeilaiModel::editCourseItem($where1,$data1);
        $res2 = BeilaiModel::editCourseItem($where2,$data2);

        if (!$res  || !$res2){
            show_json(0,['msg'=>'操作失败']);
        }else{
            show_json(1,['msg'=>'操作成功']);
        }
    }
}